package org.iwidget.desktop.model;
import java.util.Observable;
import org.w3c.dom.*;
/**
 *
 * @author Muhammad Hakim A
 */

public class ElementWindow extends Observable
{

    public ElementWindow()
    {
        height = 0;
        opacity = 100;
        title = "";
        visible = true;
        width = 0;
        x = 0;
        y = 0;
        setChanged();
    }

    public ElementWindow(WidgetElement widget)
    {
        height = 0;
        opacity = 100;
        title = "";
        visible = true;
        width = 0;
        x = 0;
        y = 0;
        this.widget = widget;
        setChanged();
    }

    public ElementWindow(WidgetElement widget, String name)
    {
        height = 0;
        opacity = 100;
        title = "";
        visible = true;
        width = 0;
        x = 0;
        y = 0;
        this.widget = widget;
        this.name = name;
    }

    public void clearChange()
    {
        clearChanged();
    }

    public int getHeight()
    {
        return height;
    }

    public void setHeight(int i)
    {
        setChanged();
        height = i;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String s)
    {
        name = s;
    }

    public int getOpacity()
    {
        return opacity;
    }

    public void setOpacity(int i)
    {
        setChanged();
        opacity = i;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String s)
    {
        title = s;
    }

    public boolean getVisible()
    {
        return visible;
    }

    public void setVisible(boolean b)
    {
        setChanged();
        visible = b;
    }

    public int getWidth()
    {
        return width;
    }

    public void setWidth(int i)
    {
        setChanged();
        width = i;
    }

    public int getX()
    {
        return x;
    }

    public void setX(int i)
    {
        setChanged();
        x = i;
    }

    public int getY()
    {
        return y;
    }

    public void setY(int i)
    {
        setChanged();
        y = i;
    }

    public void parse(Node windowNode)
        throws Exception
    {
        NamedNodeMap map = windowNode.getAttributes();
        for(int iCtr = 0; iCtr < map.getLength(); iCtr++)
        {
            Node attr = map.item(iCtr);
            parseHelper(attr.getNodeName(), attr.getNodeValue());
        }

        NodeList nodes = windowNode.getChildNodes();
        for(int iCtr = 0; iCtr < nodes.getLength(); iCtr++)
        {
            Node childNode = nodes.item(iCtr);
            String nodeName = childNode.getNodeName();
            String nodeValue = childNode.getNodeValue();
            if(childNode.getFirstChild() != null)
                parseHelper(nodeName, childNode.getFirstChild().getNodeValue());
        }

    }

    private void parseHelper(String nodeName, String value)
    {
        if(nodeName.equals("height"))
            setHeight(Integer.parseInt(value));
        else
        if(nodeName.equals("name"))
            setName(value);
        else
        if(nodeName.equals("opacity"))
            setOpacity(Integer.parseInt(value));
        else
        if(nodeName.equals("visible"))
        {
            if(value.equals("true"))
                setVisible(true);
            else
                setVisible(false);
        } else
        if(nodeName.equals("width"))
            setWidth(Integer.parseInt(value));
        else
        if(nodeName.equals("x"))
            setX(Integer.parseInt(value));
        else
        if(nodeName.equals("y"))
            setY(Integer.parseInt(value));
    }

    private int height;
    private String name;
    private int opacity;
    private String title;
    private boolean visible;
    private int width;
    private int x;
    private int y;
    public WidgetElement widget;
}